package ua.kpi.apolonov.parsers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import ua.kpi.apolonov.answers.Answer;
import ua.kpi.apolonov.questions.FileQuestionStructure;
import ua.kpi.apolonov.factories.ConcreteQuestionTypeFactory;
import ua.kpi.apolonov.normalizers.AnswersNormalizers;
import ua.kpi.apolonov.questions.Question;

/**
 * Parsers for different types of the data
 * @version 2.0 18.03.2013
 * @author Yegor Apolonov
 *
 */

public class Parsers implements Serializable {
	
	private final String FILE_REGEX = "\\|";
	private final String USER_ANSWER_REGEX = "\\s";
	private AnswersNormalizers normalizer;
	private ConcreteQuestionTypeFactory questionsTypeFactory = new ConcreteQuestionTypeFactory();
		
	public String[] parseFileLine(String line) {
		return line.split(FILE_REGEX);
	}
	
	public String[] parseTextAnswer(String userAnswers) {
		return userAnswers.toLowerCase().split(USER_ANSWER_REGEX);
    }
	
	public int[] toIntArrayUserAnswer(String userAnswers) {
		String[] answerElements = parseTextAnswer(userAnswers);
		int[] userNumbers = new int[answerElements.length];
		try {
            for(int elementPosition = 0; elementPosition < answerElements.length; elementPosition++) {
                 userNumbers[elementPosition] = Integer.parseInt(answerElements[elementPosition]);
            }
        } catch(NumberFormatException e) {
        	System.out.println("\n Вы некорректно ввели ответ. Ответ не засчитан!");
        }
		return userNumbers;
	}
	
	public Question parseFileQuestion(String[] detailsOfQuestion) {
		FileQuestionStructure questionStructure = new FileQuestionStructure(detailsOfQuestion);
		String questionType = questionStructure.getQuestionType();
		normalizer = new AnswersNormalizers(detailsOfQuestion);
		List<Answer> answer = new ArrayList<Answer>();
		
		if(questionType.equalsIgnoreCase("multiple")) {
			questionStructure.setTrueAnswersNumber(Integer.parseInt(detailsOfQuestion[1]));
			questionStructure.setOffsetToAnswers(3);
			questionStructure.recalculateAnswersSize();
			for(Answer normalAnswer : normalizer.NumericAnswerNormalizer(questionStructure)) {
				answer.add(normalAnswer);
			}
			return questionsTypeFactory.createMultipleQuestionType(detailsOfQuestion[2], answer);
		} 
		else if(questionType.equalsIgnoreCase("single")) {
			answer = normalizer.NumericAnswerNormalizer(questionStructure);
			return questionsTypeFactory.createSingleQuestionType(detailsOfQuestion[1], answer);
		}
		else if(questionType.equalsIgnoreCase("yesno")) {
			answer = normalizer.YesNoAnswerNormalizer(questionStructure);
			return questionsTypeFactory.createYesNoQuestionType(detailsOfQuestion[1], answer);
		}
		
		else if(questionType.equalsIgnoreCase("text")) {
			answer = normalizer.DefaultNormalizer(questionStructure);
			return questionsTypeFactory.createTextQuestionType(detailsOfQuestion[1], answer);
		}
		
		else if(questionType.equalsIgnoreCase("range")) {
			answer = normalizer.DefaultNormalizer(questionStructure);
			return questionsTypeFactory.createRangeQuestionType(detailsOfQuestion[1], answer);
		}
		return null;
	}
	
}
